﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;

namespace WebApp
{
    public class Global : System.Web.HttpApplication
    {
        public static IUnityContainer s_unityContainer;

        protected void Application_Start(object sender, EventArgs e)
        {
            s_unityContainer = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            //section.Containers["containerOne"].Configure(myContainer);
            section.Configure(s_unityContainer, "cwt");

            //System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
            ////builder.DataSource = "192.168.1.34";
            //builder.DataSource = ".";
            //builder.InitialCatalog = "cwt";
            //builder.UserID = "sa";
            //builder.Password = "12345678";
            //// Persist Security Info=true
            //builder.PersistSecurityInfo = true;
            ////string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\yliu\smartspace\SsTool\DB\ssdb.mdf;Integrated Security=True;User Instance=True";
            ////string connectionString = ConfigurationManager.ConnectionStrings["SsTool.Fields.Settings.ssdbConnectionString"].ConnectionString;
            //string connectionString = builder.ConnectionString;
            ////s_unityContainer.RegisterInstance<SsDbContext>(new SsDbContext(connectionString));
            ////s_unityContainer.RegisterInstance<ContextFactory>(new ContextFactory(connectionString));
            //s_unityContainer.RegisterInstance<SmartSpace.Cwt.CwtFactory>(new SmartSpace.Cwt.CwtFactory(connectionString));
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

        public static T Get<T>()
        {
            return s_unityContainer.Resolve<T>();
        }
    }
}